import PD from './index';

/**
 * 与业务相关的辅助方法
 */
import { APP_ROUTES } from "../enum/app-routes";
import Store from '@/store';
import { MINI_ENV_VERSION } from './wx-types';
export namespace PdHelp {
	/**
	 * 跳转接单宝
	 */
	export function navToJDB (path = 'pages/index/index') {
		PD.UniHelp.navToMini({
			appId: PD.Constant.JDB_APPID,
			path,
			envVersion : import.meta.env.MODE === 'development' ? 'develop' : 'release'
		});
	}
	/**
	 * 跳转到好管家
	 */
	export function navToHGJ (path = 'pages/index/index') {
		PD.UniHelp.navToMini({
			appId: PD.Constant.HGJ_APPID,
			path,
			envVersion : import.meta.env.MODE === 'development' ? 'develop' : 'release'
		});
	}
	/**
	 * 跳转到坐个车
	 */
	export function navToZGC (path = 'pages/index/index') {
		PD.UniHelp.navToMini({
			appId: PD.Constant.ZGC_APPID,
			path,
			envVersion : import.meta.env.MODE === 'development' ? 'develop' : 'release'
		});
	}
	/**
	 * 获取cos上的静态资源
	 * @param {String} path 图片资源在imgs目录下的路径
	 * @returns {String} imgUrl 图片url地址
	 */
	export function getCosImg (path : string) : string {
		const resourcesFolederName = import.meta.env.NODE_ENV === 'production' ? 'resources' : 'test-resources';
		return `${import.meta.env.VITE_PD_BUCKET_URI}/${resourcesFolederName}/imgs/${path}`;
	}
	
	/**
	 * 地址信息填充
	 * 规则：如果地址内没有（省/市）字段，填充进去
	 */
	export function locationAddressPad (address : string, province : string, city : string) : string {
		let s = address;
		if (!address.includes(city)) {
			s = city + s;
		}
		if (!address.includes(province)) {
			s = province + s;
		}
		return s;
	}
	
	/**
	 * @return {boolean} 是否停留在当前页面 
	 */ 
	export function isInCurrentPage () : boolean {
		return getCurrentPages()[0].route === PD.UniHelp.getInsByDelta()?.route;
	}
	
	/**
	 * 获取小程序跳转的版本
	 */
	export function getMiniEnvVersion () {
		return {
			development : MINI_ENV_VERSION.develop,
			beta : MINI_ENV_VERSION.trial,
			production : MINI_ENV_VERSION.release
		}[import.meta.env.MODE];
	};
	
	/**
	 * 是否从入口页进入的小程序
	 */
	export function isOpenFromEntry () : boolean {
		return getCurrentPages()[0].route === APP_ROUTES.index;
	};

	/**
	 * 获取所有页面在pages.json中的配置
	 * @returns {Array} 页面配置对象一维数组
	 */
	export function getAllPageInJson () : Array<typeof PD.PagesJson.pages[0]> {
		const main = PD.PagesJson.pages;

		const subs = PD.PagesJson.subPackages.reduce((cur, next) => {
			return [...cur, ...next.pages.map(v => {
				return {
					...v,
					path : `${next.root}/${v.path}`,
				};
			})];
		}, [] as typeof PD.PagesJson.pages);
		return [...main, ...subs];
	}
	
	/**
	 * 获取某个页面在page.json中的配置
	 * @param {APP_ROUTES} route 
	 * @return 
	 */ 
	export function getPageConfigInJson (route : APP_ROUTES) : typeof PD.PagesJson.pages[number] | undefined {
		return getAllPageInJson().find(item => item.path === route);
	}
	
	//获取某个页面在pages.json中的style配置
	export function getPageStyleInJson (route : APP_ROUTES) : typeof PD.PagesJson.pages[number]['style'] | undefined {
		return getPageConfigInJson(route)?.style;
	}

	/**
	 * 页面是否用了自定义导航
	 * @param {APP_ROUTES} route 某个页路径，非必填，默认值为当前页面路径
	 */
	export function isUseCustomNav (route ?: APP_ROUTES) : boolean {
		const path = route || PD.UniHelp.getCurrentPageIns()?.route;
		if (!path) {
			return false;
		};
		return !path ? false : getPageStyleInJson(path as APP_ROUTES)?.navigationStyle === 'custom';
	}
	
	export function isPageInTab (route ?: APP_ROUTES) : boolean {
		const path = route || PD.UniHelp.getCurrentPageIns()?.route;
		if (!path) {
			return false;
		};
		return PD.PagesJson.tabBar.list.some(v => v.pagePath === path);
	}
	
    /**
	 * 获取接bucket中的资源
	 * @params {path : 资源路径(不带域名)}
	 * @returns {resoucePath : 拼接完成后的资源路径}
	 */
	export function getBucketResource (path : string,) : string {
		return `${import.meta.env.VITE_PD_BUCKET_URI}/resources/${path}`;
	}

	/**
	 * 
	 */
	export function callPlatformService () {
		const serviceConfig = PD.AppDeclare.getCurrentApp()!.workWechatConfig.serviceConfig;
		// eslint-disable-next-line @typescript-eslint/no-explicit-any 
		(wx as any).openCustomerServiceChat({
			extInfo : {
				url : serviceConfig.url
			},
			corpId : serviceConfig.corpId,
			fail : () => {
				//企微客服唤起失败，则拨打后台配置的客服电话
				Store.serverAppConfig().haoguanjiaServiceMobile && PD.UniHelp.makePhoneCall(String(Store.serverAppConfig().haoguanjiaServiceMobile));
				Store.serverAppConfig().jiedanbaoServiceMobile && PD.UniHelp.makePhoneCall(String(Store.serverAppConfig().jiedanbaoServiceMobile));
			}
		});
	}

	/**
	 * 小程序scece转对象
	 * @param {string} paramString 
	 */ 
	export function scece2object <T>(paramString : string) : T {
		if (!paramString || !paramString.length) {
			return {} as T;
		};
		const obj = {};
		const paramsArr = paramString.split('&');
		for (let i = 0; i < paramsArr.length; i ++) {
			const arr = paramsArr[i].split('=');
			obj[arr[0]] = arr[1];
		};
		return obj as T;
	}
}